<?php

namespace Alipay;

use Yii;
use yii\base\Exception;
use Alipay\aop\AopClient;
use Alipay\aop\SignData;
use Alipay\EncryptParseItem;
use common\helpers\ArrayHelper;

/**
 * 支付宝支付SDK
 * @param string $str
 * @return string
 */
class Alipayment extends \yii\base\Component {

    /**
     * @var string 应用ID
     */
    public $partners;

    /**
     * 多商户支付调用
     * @param string $partner
     * @throws InvalidConfigException
     */
    public function transport($partner) {
        $options = ArrayHelper::getValue($this->partners, $partner, []);
        $config = [
            'class' => AopClient::class,
            'signType' => 'RSA2',
        ];
        return $this->createTransport(ArrayHelper::merge($config, $options));
    }

    /**
     * Creates Swift library object, from given array configuration.
     * @param array $config object configuration
     * @return Object created object
     * @throws \yii\base\InvalidConfigException on invalid configuration.
     */
    protected function createTransport(array $config) {
        if (!isset($config['class'])) {
            throw new InvalidConfigException('Object configuration must be an array containing a "class" element.');
        }
        $classname = $config['class'];
        if (basename(str_replace('\\', '/', $classname)) === $classname) {
            $config['class'] = static::TRANSPORT_NAMESPACE . '\\' . ucfirst($classname);
        }
        return Yii::createObject($config, [$this]);
    }

}
